﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Diagnostics;
using System.Security;
using System.IO;



namespace TicketM.Configuration
{
    public partial class Configuration : System.Web.UI.Page
    {
        XmlDocument doc;
        private string ProcessConfigPath;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Process.GetCurrentProcess().ProcessName.ToLower().Contains("iisexpress"))
            {
                ProcessConfigPath = "/Configuration/TicketProcessingService.exe.config";
            }
            else
            {
                ProcessConfigPath = "/TicketM/Configuration/TicketProcessingService.exe.config";
            }
            if (!Page.IsPostBack)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Server.MapPath(ProcessConfigPath));
                XmlNode root = doc.DocumentElement;
                Tfs.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Tfs']").Attributes["value"].Value;
                TPC.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Collection']").Attributes["value"].Value;
                TP.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Project']").Attributes["value"].Value;
                TfsAdmin.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='UserName']").Attributes["value"].Value;
                TfsAdminPwd.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Password']").Attributes["value"].Value;
                ADDomain.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Domain']").Attributes["value"].Value;
                ExServer.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='ExchServerName']").Attributes["value"].Value;
                ExVer.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='ExVer']").Attributes["value"].Value;
                HelpDeskEmail.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='HelpDeskUser']").Attributes["value"].Value;
                HelpDeskPassword.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='HelpDeskPassword']").Attributes["value"].Value;
                //Periodicity.Text = root.SelectSingleNode("/configuration/appSettings/add[@key='Periodicity']").Attributes["value"].Value;
            }
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateNotificationConfig();
                UpdateProcessorConfig();                
            }
            catch (Exception ex)
            {
                Logger.CreateLogEntry("!!ERROR in Configuration.aspx.cs while updating the configurations :" + ex.Message);
                Response.Redirect("../CommonErrorPage.aspx");
            }

            
        }
        private void UpdateNotificationConfig()
        {
            doc = new XmlDocument();
            doc.Load(Path.GetPathRoot(Environment.SystemDirectory) + @"\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin\plugins\TicketNotification.dll.config");
            ReplaceValue("ExServer", ExVer.SelectedItem.Text);
            ReplaceValue("Tfs", Tfs.Text);
            ReplaceValue("Collection", TPC.Text);
            ReplaceValue("Project", TP.Text);
            ReplaceValue("User", TfsAdmin.Text);
            ReplaceValue("Password", TfsAdminPwd.Text);
            ReplaceValue("Domain", ADDomain.Text);
            doc.Save(Path.GetPathRoot(Environment.SystemDirectory) + @"\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin\plugins\TicketNotification.dll.config");
            Logger.CreateLogEntry("** INFO Notification Settings updated at " + DateTime.Now.ToString());
        }
        private void ReplaceValue(string _key, string value)
        {
            if (value != "" || value != null)
            {
                XmlNode node = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='" + _key + "']");
                node.Attributes["value"].Value = value;
            }
        }
        private unsafe SecureString GetPwd(string passwd)
        {
            SecureString pwd;
            char[] pwdArr = passwd.ToCharArray();
            fixed (char* pwdRef = pwdArr)
            {
                pwd = new SecureString(pwdRef, pwdArr.Length);
            }
            return pwd;
        }
        private void UpdateProcessorConfig()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(ProcessConfigPath));
            XmlNode root = doc.DocumentElement;
            root.SelectSingleNode("/configuration/appSettings/add[@key='Tfs']").Attributes["value"].Value = Tfs.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='Collection']").Attributes["value"].Value = TPC.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='Project']").Attributes["value"].Value = TP.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='UserName']").Attributes["value"].Value = TfsAdmin.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='Password']").Attributes["value"].Value = TfsAdminPwd.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='Domain']").Attributes["value"].Value = ADDomain.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='ExchServerName']").Attributes["value"].Value = ExServer.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='ExVer']").Attributes["value"].Value = ExVer.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='HelpDeskUser']").Attributes["value"].Value = HelpDeskEmail.Text;
            root.SelectSingleNode("/configuration/appSettings/add[@key='HelpDeskPassword']").Attributes["value"].Value = HelpDeskPassword.Text;
            //root.SelectSingleNode("/configuration/appSettings/add[@key='Periodicity']").Attributes["value"].Value = Periodicity.Text;
            doc.Save(HttpContext.Current.Server.MapPath(ProcessConfigPath));
            Logger.CreateLogEntry("** INFO Ticket Processor Service Settings updated at " + DateTime.Now.ToString());
        }
    }
}